/**
 * Bamboo Software Organization
 * Java open source software.  
 */
package org.bamboo.easyejb.demo.client;

import junit.framework.TestCase;

import org.bamboo.easyejb.client.EJBConnection;
import org.bamboo.easyejb.client.EasyEJBConfigUtil;

/**
 * @author Ramond KK He, raymond.kk.he@gmail.com 
 * @version 1.0 
 * 2008-11-27  
 */
public class HelloWorldClientDemo extends TestCase {
		
//	private static Hashtable initialContextProperties ;
//	public static Hashtable getInitialContextProperties() {
//		
//		if(initialContextProperties!=null)
//			return initialContextProperties;
//		InputStream is = null;
//		Properties ps = null;
//		try {
//			is = HelloWorldClientDemo.class.getResourceAsStream("/easyejb.jndi.properties");
//			
//			ps = new Properties();
//			ps.load(is);
//			
//			try{	initialContextProperties = new Hashtable(3);
//				//test in jboss4.0.0
//				//also can be tested with remote WAS, Weblogic etc.
//				System.out.println("ps  " + ps);
//				if(ps.getProperty("java.naming.factory.initial")==null)
//					throw new IllegalArgumentException("java.naming.factory.initial not set in jndi.properties!");
//				if(ps.getProperty("java.naming.provider.url")==null)
//					throw new IllegalArgumentException("java.naming.provider.url not set in jndi.properties!");
//				
//				initialContextProperties.put("java.naming.factory.initial",ps.getProperty("java.naming.factory.initial"));
//				initialContextProperties.put("java.naming.provider.url", ps.getProperty("java.naming.provider.url"));
//				return initialContextProperties;
//				
//			} catch (Throwable e) {	
//				System.err.println("Initcontext error: " + e);
//				e.printStackTrace();
//			}
//		} catch (Exception e) {	
//			System.err.println("[WARN] jndi.properties no found! Your should set jndi context by -D or in code.  " + e);
//		}		
//		return null;
//	}
//	
	public void testRemoteEJB() {		
	
		
		EJBConnection ejbConnection = new EJBConnection( EasyEJBConfigUtil.getInitialContextProperties() );	
		
		Object[] ags = new Object[] {"Obama"};
		try {
			Object result = ejbConnection.invokeEJB("org.bamboo.easyejb.demo.helloworld.ejb.HelloWorld","sayHello",ags);
			System.out.println("result: " + result);
			
			assertEquals("HelloWorldBean:Hello Obama", result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
//	/**
//	 * test no method match
//	 *
//	 */
//	public void testRemoteEJB2() {		
//		EJBConnection ejbConnection = new EJBConnection(getInitialContextProperties());
//		
//		Object[] ags = new Object[] {"Obama"};
//		try {
//			Object s = ejbConnection.invokeEJB("org/bamboo/easyejb/demo/helloworld/ejb/HelloWorld","sayGoodBye",ags);
//			System.out.println("result " + s);
//			fail("java.lang.NoSuchMethodException should be thrown!");
//		} catch (Exception e) {
//			System.out.println("exception:" + e);
//			assertNotNull(e);
//		}
//	}
//	
//	/**
//	 * test wrong number args
//	 *
//	 */
//	public void testRemoteEJB3() {			
//		EJBConnection ejbConnection = new EJBConnection(getInitialContextProperties());
//		
//		Object[] ags = new Object[] {"Obama","Hillary"};
//		try {
//			Object s = ejbConnection.invokeEJB("org.bamboo/easyejb/demo/helloworld/ejb/HelloWorld","sayHello",ags);
//			System.out.println("result " + s);
//			fail("java.lang.NoSuchMethodException should be thrown!");
//		} catch (Exception e) {
//			System.out.println("exception:" + e);
//			assertNotNull(e);
//		}
//	}
//	
//	/**
//	 * test with incompatible args
//	 *
//	 */
//	public void testRemoteEJB4() {	
//		EJBConnection ejbConnection = new EJBConnection( getInitialContextProperties() );
//		
//		Object[] ags = new Object[] {new Double(112.23)};
//		try {
//			Object result = ejbConnection.invokeEJB("org.bamboo.easyejb.demo.helloworld.ejb.HelloWorld","sayHello",ags);
//			assertNotNull(result);
//			fail("java.lang.NoSuchMethodException should be thrown!");
//		} catch (Exception e) {
//			System.out.println("exception:" + e);
//			assertNotNull(e);
//		}
//	}
	public static void main(String[] args) {		
		//TestRunner.run(HelloWorldClientDemo.class);
		
		HelloWorldClientDemo helloWorldClientDemo = new HelloWorldClientDemo();
		helloWorldClientDemo.testRemoteEJB();
	}
}
